using System;
using System.Data;
using System.Windows.Forms;
namespace mywine.Class
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CWine : TUnit.Class.Class
    {
        #region Declares
        int _wineid = -1;
        string _winename = "";
        double _summaryscore = 0.0;
        double _valuescore = 0.0;
        double _price = 0.0;
        int _storeid = -1;
        int _year = -1;
        bool _allprice = false;
        bool _showtop = false;

        public bool Showtop
        {
            get { return _showtop; }
            set { _showtop = value; }
        }
        int _winecolorid = -1;

        public int Winecolorid
        {
            get { return _winecolorid; }
            set { _winecolorid = value; }
        }
        string _style = "";
        string _shop = "";

        public string Shop
        {
            get { return _shop; }
            set { _shop = value; }
        }

        CType _type = null;
        /// <summary>
        /// 
        /// </summary>
        public CType Type
        {
            get {
                if (_type == null)
                    _type = new CType(Login);
                return _type; }
            set { _type = value; }
        }

        string _description = "";

        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        #endregion

        #region Constructors
        public CWine()
        {
        }
        public CWine(TUnit.Class.CLogin Login)
        {
            base.Login = Login;
        }
        public CWine(TUnit.Class.CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBWine db = new AccDBWine())
            {
                this.Populate((CWine)db.GetClass(Login, ObjId));
            }
        }
        public CWine(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CWine(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBWine db = new AccDBWine())
            {
                this.Populate((CWine)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(TUnit.Data.IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("wineid") >= 0)
                    {
                        this._wineid = (dr["wineid"] != DBNull.Value ? Convert.ToInt32(dr["wineid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD wineid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("winename") >= 0)
                    {
                        this._winename = (dr["winename"] != DBNull.Value ? Convert.ToString(dr["winename"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD winename [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("mYear") >= 0)
                    {
                        this._year = (dr["mYear"] != DBNull.Value ? Convert.ToInt32(dr["mYear"]) : 1900);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD storeid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("summaryscore") >= 0)
                    {
                        this._summaryscore = (dr["summaryscore"] != DBNull.Value ? Convert.ToDouble(dr["summaryscore"]) : 0.0);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD summaryscore [numeric](18, 0) NULL ");
                    //}
                    if (dr.Table.Columns.IndexOf("valuescore") >= 0)
                    {
                        this._valuescore = (dr["valuescore"] != DBNull.Value ? Convert.ToDouble(dr["valuescore"]) : 0.0);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD valuescore [numeric](18, 0) NULL ");
                    //}
                    if (dr.Table.Columns.IndexOf("price") >= 0)
                    {
                        this._price = (dr["price"] != DBNull.Value ? Convert.ToDouble(dr["price"]) : 0.0);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD price [numeric](18, 0) NULL ");
                    //}
                    if (dr.Table.Columns.IndexOf("storeid") >= 0)
                    {
                        this._storeid = (dr["storeid"] != DBNull.Value ? Convert.ToInt32(dr["storeid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD storeid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("winecolorid") >= 0)
                    {
                        this._winecolorid = (dr["winecolorid"] != DBNull.Value ? Convert.ToInt32(dr["winecolorid"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD storeid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("locaid") >= 0)
                    {
                        this._type = new CType(Login, p, (dr["locaid"] != DBNull.Value ? Convert.ToInt32(dr["locaid"]) : -1));
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD storeid [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("shop") >= 0)
                    {
                        this._shop = (dr["shop"] != DBNull.Value ? Convert.ToString(dr["shop"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD style [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("style") >= 0)
                    {
                        this._style = (dr["style"] != DBNull.Value ? Convert.ToString(dr["style"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD style [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("allprice") >= 0)
                    {
                        this._allprice = (dr["allprice"] != DBNull.Value ? Convert.ToBoolean(dr["allprice"]) : false);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD allprice [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("showtop") >= 0)
                    {
                        this._showtop = (dr["showtop"] != DBNull.Value ? Convert.ToBoolean(dr["showtop"]) : false);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD showtop [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("description") >= 0)
                    {
                        this._description = (dr["description"] != DBNull.Value ? Convert.ToString(dr["description"]) : "Sorry! No data.");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tbl_wines ADD winename [nvarchar] (50) NULL");
                    //}
                }
                catch { }
            }
        }
        
        public void Populate(CWine Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._wineid = Obj.wineid;
                    this._winename = Obj.winename;
                    this._summaryscore = Obj.summaryscore;
                    this._valuescore = Obj.valuescore;
                    this._price = Obj.price;
                    this._storeid = Obj.storeid;
                    this._style = Obj.style;
                    this._type = Obj.Type;
                    this._allprice = Obj.Allprice;
                    this._showtop = Obj.Showtop;
                    this._description = Obj.Description;
                }
                catch { }
            }
        }

        public override bool Save()
        {
            using (TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBWine db = new AccDBWine())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }

        public override bool Save(TUnit.Data.IProvider p)
        {
            using (AccDBWine db = new AccDBWine())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public override bool Delete()
        {
            using (TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBWine db = new AccDBWine())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }

        public override bool Delete(TUnit.Data.IProvider p)
        {
            using (AccDBWine db = new AccDBWine())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public string toString()
        {
            return
                this.wineid.ToString() + "|"
                + this.winename + "|"
                + "" + this.Year.ToString() + "|"
                + "$" + this.price.ToString() + "|"
                + "" + this.summaryscore.ToString() + "|"
                + "" + this.valuescore.ToString() + "|"
                + "" + this.Type.Typename + "";
        }

        public static string toString(TUnit.Class.CCollection Collection, string pricerange)
        {
            string str = "";
            if (Collection != null)
            {
                int count = 0;
                for (int i = 0; i < Collection.Count; i++)
                {
                    switch (pricerange)
                    {
                        case "UN7":
                            {
                                CWine o = (CWine)Collection[i];
                                if (o.price < 7 && count < 5)
                                {
                                    count++;
                                    str += "@WINE";
                                    str += o.toString();
                                }
                            }
                            break;
                        case "F7T15":
                            {
                                CWine o = (CWine)Collection[i];
                                if (o.price >= 7 && o.price < 15 && count < 5)
                                {
                                    count++;
                                    str += "@WINE";
                                    str += o.toString();
                                }
                            }
                            break;
                        case "F15T25":
                            {
                                CWine o = (CWine)Collection[i];
                                if (o.price >= 15 && o.price < 25 && count < 5)
                                {
                                    count++;
                                    str += "@WINE";
                                    str += o.toString();
                                }
                            }
                            break;
                        case "OV25":
                            {
                                CWine o = (CWine)Collection[i];
                                if (o.price >= 25 && count < 5)
                                {
                                    count++;
                                    str += "@WINE";
                                    str += o.toString();
                                }
                            }
                            break;
                        case "ALL":
                            {
                                CWine o = (CWine)Collection[i];
                                if (o.Allprice && count < 5)
                                {
                                    count++;
                                    str += "@WINE";
                                    str += o.toString();
                                }
                            }
                            break;
                    }
                   
                }
            }
            if (str.StartsWith("@WINE"))
                str = str.Substring(5);

            return str;
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CWine obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CWine Wine);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CWine Wine);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CWine Wine);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CWine Wine);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CWine obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int wineid
        {
            get { return this._wineid; }
            set
            {
                if (this._wineid != value)
                {
                    this._wineid = value;
                }
            }
        }
        public string winename
        {
            get { return this._winename; }
            set
            {
                if (this._winename != value)
                {
                    this._winename = value;
                }
            }
        }
        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }
        public double summaryscore
        {
            get { return this._summaryscore; }
            set
            {
                if (this._summaryscore != value)
                {
                    this._summaryscore = value;
                }
            }
        }
        public double price
        {
            get { return this._price; }
            set
            {
                if (this._price != value)
                {
                    this._price = value;
                }
            }
        }
        public double valuescore
        {
            get { return this._valuescore; }
            set
            {
                if (this._valuescore != value)
                {
                    this._valuescore = value;
                }
            }
        }
        public int storeid
        {
            get { return this._storeid; }
            set
            {
                if (this._storeid != value)
                {
                    this._storeid = value;
                }
            }
        }
        public string style
        {
            get { return this._style; }
            set
            {
                if (this._style != value)
                {
                    this._style = value;
                }
            }
        }
        public bool Allprice
        {
            get { return _allprice; }
            set { _allprice = value; }
        }
        System.Drawing.Bitmap _icon = null;
        /// <summary>
        /// 
        /// </summary>
        public System.Drawing.Bitmap Icon
        {
            get
            {
                if (_icon == null)
                {
                    _icon = new System.Drawing.Bitmap(Application.StartupPath + "\\Icons\\check3.png");
                }
                return _icon;
            }
            set { _icon = value; }
        }

        bool _Checked = true;

        public bool Checked
        {
            get { return _Checked; }
            set
            {
                if (_Checked != value || _icon == null)
                {
                    if (value)
                        _icon = new System.Drawing.Bitmap(Application.StartupPath + "\\Icons\\Checked.png");
                    else
                        _icon = new System.Drawing.Bitmap(Application.StartupPath + "\\Icons\\unChecked.png");

                    _Checked = value;
                }
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBWine : TUnit.Class.AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBWine()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(TUnit.Class.CLogin Login, ComboBox cmb, string extName)
        {
            TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT wineid, WineName";
                sql += " FROM  tbl_wines";
                sql += " WHERE (wineid >= 0)";
                sql += " ORDER BY WineName";
                DataTable table = p.OpenTable(sql, "tbl_wines");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["WineName"] = extName;
                        row["wineid"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "WineName";
                    cmb.ValueMember = "wineid";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override TUnit.Class.Class GetClass(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tbl_wines WHERE (wineid = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tbl_wines");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CWine(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(TUnit.Data.IProvider p, TUnit.Class.Class obj)
        {
            CWine Wine = (CWine)obj;
            string sql = "SELECT * FROM tbl_wines";
            sql += " WHERE wineid =" + Wine.wineid + "";
            //sql +=" AND CompanyId =" + Wine.CompanyId + "";
            DataTable dt = p.OpenTable(sql, "tbl_wines");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && Wine.wineid != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    Wine.wineid = Convert.ToInt32(p.InitIDKey("wineid", "tbl_wines", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["wineid"] = Wine.wineid;
                row["winename"] = Wine.winename;
                row["summaryscore"] = Wine.summaryscore;
                row["mYear"] = Wine.Year;
                row["valuescore"] = Wine.valuescore;
                row["price"] = Wine.price;
                row["storeid"] = Wine.storeid;
                row["winecolorid"] = Wine.Winecolorid;
                row["style"] = Wine.style;
                row["locaid"] = Wine.Type.typeid;
                row["allprice"] = Wine.Allprice;
                row["Description"] = Wine.Description;
                if (!p.UpdateTable("tbl_wines", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(TUnit.Data.IProvider p, TUnit.Class.Class obj)
        {
            CWine Wine = (CWine)obj;
            string sql = "DELETE FROM tbl_wines";
            sql += " WHERE wineid =" + Wine.wineid + "";
            //sql +=" AND CompanyId =" + Wine.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(TUnit.Class.CLogin Login, TUnit.Data.IProvider p, TUnit.Class.CCollection Collection)
        {
            string sql = "SELECT * FROM tbl_wines WHERE winecolorid =-1 ORDER BY shop, WineName";
            DataTable table = p.OpenTable(sql, "tbl_wines");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CWine o = new CWine(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(TUnit.Data.IProvider p, TUnit.Class.Class obj, TUnit.Class.CCollection Collection)
        {
            if (obj is CWCTypes)
            {
                if (((CWCTypes)obj).Tag != null && ((CWCTypes)obj).Tag.ToString() == "RATE")
                {
                    string sql = "SELECT * FROM tbl_wines WHERE (summaryscore=0) AND (locaid ={0}) AND (winecolorid={1}) ORDER BY valuescore DESC, winename";
                    sql = String.Format(sql, ((CWCTypes)obj).typeid, ((CWCTypes)obj).winecolorid);
                    DataTable table = p.OpenTable(sql, "tbl_wines");
                    if (table != null)
                    {
                        foreach (DataRow row in table.Rows)
                        {
                            CWine o = new CWine(obj.Login, p, row);
                            Collection.Add(o);
                        }
                    }
                }
                else
                {
                    //Tat ca cac loai ruou do hoac trang
                    if (((CWCTypes)obj).typeid == -1 && ((CWCTypes)obj).winecolorid != -1)
                    {
                        string sql = "SELECT * FROM tbl_wines WHERE (summaryscore > 0) AND (winecolorid={0}) ORDER BY valuescore DESC, winename";
                        sql = String.Format(sql, ((CWCTypes)obj).winecolorid);
                        DataTable table = p.OpenTable(sql, "tbl_wines");
                        if (table != null)
                        {
                            foreach (DataRow row in table.Rows)
                            {
                                CWine o = new CWine(obj.Login, p, row);
                                Collection.Add(o);
                            }
                        }
                    }
                    else if (((CWCTypes)obj).winecolorid != -1)
                    {
                        string sql = "SELECT * FROM tbl_wines WHERE (summaryscore > 0) AND locaid ={0} AND winecolorid={1} ORDER BY valuescore DESC, winename";
                        sql = String.Format(sql, ((CWCTypes)obj).typeid, ((CWCTypes)obj).winecolorid);
                        DataTable table = p.OpenTable(sql, "tbl_wines");
                        if (table != null)
                        {
                            foreach (DataRow row in table.Rows)
                            {
                                CWine o = new CWine(obj.Login, p, row);
                                Collection.Add(o);
                            }
                        }
                    }
                    else
                    {
                        string sql = "SELECT  tbl_wines.*";
                        sql += " FROM  tbl_wines INNER JOIN";
                        sql += " tbl_winecolor ON tbl_wines.winecolorid = tbl_winecolor.winecolorid";
                        sql += " WHERE (summaryscore > 0) AND(locaid ={0}) AND (tbl_winecolor.storeid = {1}) ORDER BY  tbl_wines.valuescore DESC, tbl_wines.winename";
                        sql = String.Format(sql, ((CWCTypes)obj).typeid, ((CWCTypes)obj).Tag);
                        DataTable table = p.OpenTable(sql, "tbl_wines");
                        if (table != null)
                        {
                            foreach (DataRow row in table.Rows)
                            {
                                CWine o = new CWine(obj.Login, p, row);
                                Collection.Add(o);
                            }
                        }
                    }
                }
            }
            if (obj is CType)
            {
                string sql = "SELECT * FROM tbl_wines WHERE typeid ={0}";
                sql = String.Format(sql, ((CType)obj).typeid);
                DataTable table = p.OpenTable(sql, "tbl_wines");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CWine o = new CWine(obj.Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
            if (obj is CStore)
            {
                string sql = "SELECT * FROM tbl_wines WHERE storeid ={0}";
                sql = String.Format(sql, ((CStore)obj).storeid);
                DataTable table = p.OpenTable(sql, "tbl_wines");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CWine o = new CWine(obj.Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        /// <param name="storeid"></param>
        /// <param name="typeid"></param>
        /// <param name="style"></param>
        public void GetCollection(TUnit.Class.CLogin Login, TUnit.Class.CCollection Collection, string storeid, string typeid, string style)
        {
            TUnit.Data.IProvider p = new TUnit.Data.OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT * FROM tbl_wines WHERE storeid ={0} AND typeid ={1} AND style ='{2}'";
                sql = String.Format(sql, storeid, typeid, style);
                DataTable table = p.OpenTable(sql, "tbl_wines");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CWine o = new CWine(Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
    }
    #endregion
}
